package com.feihao.zhou.demoTreeMapTest;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class TestTreeMap {
    public static void main(String[] args) {
        TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });


        map.put(new Student("张三", 20), "湖南");
        map.put(new Student("李四", 28), "天津");
        map.put(new Student("王五", 23), "北京");
        map.put(new Student("战力", 25), "海南");
        map.put(new Student("oo力", 25), "海南");

        //lambda遍历
        map.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student student, String s) {
                System.out.println(student + "-->" + s);
            }
        });

        System.out.println("---------------------");

        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            String value = entry.getValue();
            Student key = entry.getKey();
            System.out.println(key + "--->" + value);
        }

        System.out.println("--------------------");

        Set<Student> keys = map.keySet();
        for (Student key : keys) {
            String s = map.get(key);
            System.out.println(key + "--->" + s);
        }
    }
}
